package com.colorsstudio.defence.baseClass 
{
	import com.colorsstudio.defence.ConstantValues.Constants;
	import com.colorsstudio.defence.flashExportClass.f_btnMusicOff;
	import com.colorsstudio.defence.util.singletonFactory1;
	import com.greensock.core.SimpleTimeline;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import idv.cjcat.signals.ISignal;
	import idv.cjcat.signals.Signal;
	
	/**
	 * ...
	 * @author wuzhida
	 */
	public class GUIClass extends Sprite 
	{
		public var scoreT:TextField;
		public var lifeT:TextField;
		public var moneyT:TextField;
		
		public var pauseButton:SimpleButton;
		public var settingButton:SimpleButton;
		public var speedUpButton:SimpleButton;
		
		public var signalPause:ISignal  = new Signal();
		public var signalSetting:ISignal = new Signal();
		public var signalSpeedUp:ISignal = new Signal(Number);
		
		public var player:Player = singletonFactory1(Player);
		
		public function GUIClass() 
		{
			initText();
			initButton();
		}
		
		public function initButton():void 
		{
			pauseButton = new f_btnMusic();
			settingButton = new f_btnSetting();
			speedUpButton = new f_btnSpeedUp();
			pauseButton.width  = 100;
			pauseButton.height  = 30;
			settingButton.width  = 100;
			settingButton.height  = 30;
			speedUpButton.width  = 100;
			speedUpButton.height  = 30;			
			pauseButton.x  = 50;
			pauseButton.y  = Constants.SCREEN_HEIGHT - pauseButton.height;			
			settingButton.x  = 50+pauseButton.width;
			settingButton.y  = Constants.SCREEN_HEIGHT -  pauseButton.height;
			speedUpButton.x  = 50+pauseButton.width+settingButton.width;
			speedUpButton.y  = Constants.SCREEN_HEIGHT -  speedUpButton.height;
			this.addChild(pauseButton);
			this.addChild(settingButton);
			this.addChild(speedUpButton);
			pauseButton.addEventListener(MouseEvent.CLICK, mouseClick, false, 0, true);
			settingButton.addEventListener (MouseEvent.CLICK, mouseClick, false, 0, true);
			speedUpButton.addEventListener(MouseEvent.CLICK, mouseClick, false, 0, true);
		}
		
		private function mouseClick(e:MouseEvent):void 
		{
			if (e.target == pauseButton)
			{
				signalPause.dispatch();
			}
			else if(e.target == settingButton)
			{
				signalSetting.dispatch();
			}
			else if(e.target ==speedUpButton)
			{				
				if (Constants.TIME_SCALE == 2)
				{
					signalSpeedUp.dispatch(1); 
					Constants.TIME_SCALE = 1;
				}
				else
				{
					signalSpeedUp.dispatch(2);
					Constants.TIME_SCALE = 2;
				}
				
			}
		}
		
		public function initText():void 
		{
			var textFormat:TextFormat = new TextFormat();
			textFormat.font = "Maiandra GD";
			textFormat.size = 30;
			textFormat.color = 0xFFCC33;
			
			scoreT = new TextField();
			scoreT.defaultTextFormat = textFormat;
			//scoreT.height  = 100;
			//scoreT.width  = 250;
			scoreT.autoSize = TextFieldAutoSize.LEFT;
			scoreT.text = "Score: " + player.currentScore+" ";			
			scoreT.x = 100;
			
			lifeT = new TextField();
			lifeT.defaultTextFormat = textFormat;	
			//lifeT.height  = 100;
			//lifeT.width  = 250;
			lifeT.autoSize = TextFieldAutoSize.LEFT;
			lifeT.text = "Life: " + player.currentHealth +" ";					
			lifeT.x = 100+scoreT.width;
			
			moneyT = new TextField();
			moneyT.defaultTextFormat = textFormat;
			//moneyT.height  = 100;
			//moneyT.width  = 250;
			moneyT.autoSize = TextFieldAutoSize.LEFT;
			moneyT.text = "Coins: " + player.currentMoney+" ";			
			moneyT.x = 100+lifeT.width + scoreT.width;
						
			this.addChild(scoreT);
			this.addChild(lifeT);
			this.addChild(moneyT);
		}
		
		public function updateText():void
		{
			scoreT.text = "Score: " + player.currentScore + " ";
			scoreT.x = 100;
			lifeT.text = "Life: " + player.currentHealth +" ";
			lifeT.x = 100+scoreT.width;
			moneyT.text = "Coins: " + player.currentMoney + " ";
			moneyT.x = 100 + lifeT.width + scoreT.width;
			trace(player.currentHealth, player.currentMoney, player.currentScore);
		}
	}
}